package algorithm.sort;

import java.util.Arrays;

public class SortUtil {
    public static void evaluate(Sort sort, int numberOfNum, int minNum, int maxNum) {
        int[] nums = new int[numberOfNum];
        for (int i = 0; i < nums.length; i++) {
            nums[i] = (int) (Math.random() * (maxNum - minNum) + minNum);
        }
        evaluate(sort, nums);
    }

    public static void evaluate(Sort sort, int[] nums) {
        long beforTimemills = System.currentTimeMillis();
        sort.execute(nums);
        long afterTimemills = System.currentTimeMillis();
        System.out.println(String.format("use %s sort %s number cost %s ms", sort.getClass(), nums.length,
                (afterTimemills - beforTimemills)));
    }

    public static int[] example(Sort sort, int numberOfNum) {
        // int[] nums = generateNums(numberOfNum);
        // int[] nums = { 9, 6, 7, 6, 4, 5, 2, 0, 0, 1 };
        int[] nums = { 12, 13, 13, 20, 16, 16, 17, 18, 20, 24, 30, 29, 51, 15, 14 };
        sort.execute(nums);
        return nums;
    }

    public static int[] generateNums(int numberOfNum) {
        int[] nums = new int[numberOfNum];
        for (int i = 0; i < nums.length; i++) {
            nums[i] = (int) (Math.random() * numberOfNum);
        }
        return nums;
    }

    public static void printIntArrays(int[] nums) {
        for (int i = 0; i < nums.length; i++) {
            System.out.print(nums[i] + " ");
        }
        System.out.println();
    }

    public static boolean checkIfRight(Sort standardMethod, Sort checkedMethod, int numberOfNums) {
        int[] nums = generateNums(numberOfNums);
        int[] numsa = Arrays.copyOf(nums, nums.length);
        int[] numsb = Arrays.copyOf(nums, nums.length);
        standardMethod.execute(numsa);
        checkedMethod.execute(numsb);
        boolean result = Arrays.equals(numsa, numsb);
        if (!result) {
            System.out.println("resource: ");
            printIntArrays(nums);
            System.out.println(String.format("%s: ", standardMethod.getClass()));
            printIntArrays(numsa);
            System.out.println(String.format("%s: ", checkedMethod.getClass()));
            printIntArrays(numsb);
        }
        return result;
    }

    public static boolean checkIfRight(Sort standardMethod, Sort checkedMethod, int checkTimes, int numberOfNums) {
        for (int i = 0; i < checkTimes; i++) {
            if (!checkIfRight(standardMethod, checkedMethod, numberOfNums)) {
                return false;
            }
        }
        return true;
    }
}
